/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import model.config.MySQLQuery;
import model.pojo.*;
import model.util.MySQLProviders;
/**
 *
 * @author Admin
 */
public class NhatKyMoiBanDAO {
    
    public static ArrayList<NhatKyMoiBanPOJO> LoadDanhSach(String TenDangNhap)
    {
        ArrayList<NhatKyMoiBanPOJO> nkmb = new ArrayList<NhatKyMoiBanPOJO>();
        String sql = "Select * From NhatKyMoiBan where TenDangNhap='"+TenDangNhap+"'";
        MySQLProviders helper = new MySQLProviders();
        helper.open();
        ResultSet rs = helper.executeQuery(sql);
        try {
            while(rs.next())
            {
                NhatKyMoiBanPOJO temp = new NhatKyMoiBanPOJO();
                temp.setMaNhatKyMoiBan(rs.getInt("MaNhatKyMoiBan"));
                temp.setNickDangKy(rs.getString("NickDangKy"));
                temp.setNgayDangKy(rs.getTimestamp("NgayDangKy"));
                temp.setTinhTrangGuiTienThuong(rs.getBoolean("TinhTrangGuiTienThuong"));
                temp.setTenDangNhap(TenDangNhap);
                
                nkmb.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(NhatKyMoiBanDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            helper.close();
        }
        return nkmb;
    }
    
    
    public static ArrayList<NhatKyMoiBanPOJO> getDanhSachCoPhanTrang(int trang) throws FileNotFoundException, IOException {
      ArrayList<NhatKyMoiBanPOJO> ds = new ArrayList<NhatKyMoiBanPOJO>(); 
      int soSanPhamTrenTrang = ThamSoDAO.laySoLuongTrenTrang();
      int batDau = (trang - 1) * soSanPhamTrenTrang;
      if(soSanPhamTrenTrang==batDau)
          batDau=1;     
      //mo ket noi
      MySQLProviders helper = new MySQLProviders();
      helper.open();
      
      //doc query tu file text
      //lay cau truy van tu file text
     MySQLQuery mySQLQuery=new MySQLQuery();
     mySQLQuery.Read("PHANTRANG");
     String sql=String.format(mySQLQuery.GetQuery(MySQLQuery.QueryKey.SELECT_ALL_NHATKYMOIBAN_PHANTRANG),batDau,soSanPhamTrenTrang);
      ResultSet rs = helper.executeQuery(sql);
      try {
         while (rs.next()) {
            NhatKyMoiBanPOJO temp = new NhatKyMoiBanPOJO();
            temp.setMaNhatKyMoiBan(rs.getInt("MaNhatKyMoiBan"));
            temp.setNgayDangKy(rs.getTimestamp("NgayDangKy"));
            temp.setTinhTrangGuiTienThuong(rs.getBoolean("TinhTrangGuiTienThuong"));
            temp.setTenDangNhap(rs.getString("TenDangNhap"));
            ds.add(temp);
         }
      } catch (SQLException ex) {
         System.out.println(ex.getMessage());
      }
      helper.close();
      return ds;
   }
    
    public static NhatKyMoiBanPOJO getById(int id){
       NhatKyMoiBanPOJO obj=new NhatKyMoiBanPOJO();
       MySQLProviders helper = new MySQLProviders();
       helper.open();
       ResultSet rs = helper.executeQuery("select *from nhatkymoiban where manhatkymoiban="+id);
       try {
           while (rs.next()) {            
            obj.setMaNhatKyMoiBan(rs.getInt("MaNhatKyMoiBan"));
            obj.setNgayDangKy(rs.getTimestamp("NgayDangKy"));
            obj.setTinhTrangGuiTienThuong(rs.getBoolean("TinhTrangGuiTienThuong"));
            obj.setTenDangNhap(rs.getString("TenDangNhap"));
            
         }
       }catch(Exception e){
           
       }finally{
           helper.close();
       }
       return obj;
   }
    
    public static int Them(NhatKyMoiBanPOJO nk)
    {    
        int flag=-1;
        MySQLProviders helper = new MySQLProviders();
        helper.open();
        try
        {
            String sql = String.format("INSERT INTO `vndoan`.`nhatkymoiban` (`tendangnhap`, `nickdangky`, `ngaydangky`) "
                    + "VALUES ('"+nk.getTenDangNhap()+"','"+nk.getNickDangKy()+"','"+nk.getNgayDangKy()+"')");
            flag = helper.executeUpdate(sql);
        }
        catch(Exception ex)
        {
            Logger.getLogger(KhachHangPOJO.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            helper.close();
        }
        return flag;
    }
    
}
